﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace EPMAuditing.EventHandlers.RegisterEvents
{
    public partial class FormUpdateConfiguration : Form
    {
        private string _projectServerUrl;
        private string _assemblyName;
        private string _assemblyVersion;
        private string _assemblyCulture;
        private string _assemblyPublicKey;
        private bool _useDefaultCredentials = true;

        private bool _dirty = false;

        public FormUpdateConfiguration(string projectServerUrl, string assemblyName, string assemblyVersion, 
            string assemblyCulture, string assemblyPublicKey, bool useDefaultCredentials)
        {
            InitializeComponent();
            _projectServerUrl = projectServerUrl;
            _assemblyName = assemblyName;
            _assemblyVersion = assemblyVersion;
            _assemblyCulture = assemblyCulture;
            _assemblyPublicKey = assemblyPublicKey;
            _useDefaultCredentials = useDefaultCredentials;
            SetControlValues();
        }

        private void SetControlValues()
        {
            textBoxProjectServerUrl.Text = _projectServerUrl;
            textBoxAssemblyName.Text = _assemblyName;
            textBoxAssemblyVersion.Text = _assemblyVersion;
            textBoxAssemblyCulture.Text = _assemblyCulture;
            textBoxAssemblyPublicKey.Text = _assemblyPublicKey;
            checkBoxUseDefaultCredentials.Checked = _useDefaultCredentials;
        }

        #region Properties

        public string ProjectServerUrl
        {
            get { return _projectServerUrl; }
            set { _projectServerUrl = value; }
        }

        public string AssemblyName
        {
            get { return _assemblyName; }
            set { _assemblyName = value; }
        }

        public string AssemblyVersion
        {
            get { return _assemblyVersion; }
            set { _assemblyVersion = value; }
        }

        public string AssemblyCulture
        {
            get { return _assemblyCulture; }
            set { _assemblyCulture = value; }
        }

        public string AssemblyPublicKey
        {
            get { return _assemblyPublicKey; }
            set { _assemblyPublicKey = value; }
        }

        public bool UseDefaultCredentials
        {
            get { return _useDefaultCredentials; }
            set { _useDefaultCredentials = value; }
        }

        public bool Dirty
        {
            get { return _dirty; }
        }

        #endregion

        #region Events

        private void textBoxProjectServerUrl_TextChanged(object sender, EventArgs e)
        {
            _dirty = true;
            _projectServerUrl = textBoxProjectServerUrl.Text;
        }

        private void textBoxAssemblyName_TextChanged(object sender, EventArgs e)
        {
            _dirty = true;
            _assemblyName = textBoxAssemblyName.Text;
        }

        private void textBoxAssemblyVersion_TextChanged(object sender, EventArgs e)
        {
            _dirty = true;
            _assemblyVersion = textBoxAssemblyVersion.Text;

        }

        private void textBoxAssemblyCulture_TextChanged(object sender, EventArgs e)
        {
            _dirty = true;
            _assemblyCulture = textBoxAssemblyCulture.Text;

        }

        private void textBoxAssemblyPublicKey_TextChanged(object sender, EventArgs e)
        {
            _dirty = true;
            _assemblyPublicKey = textBoxAssemblyPublicKey.Text;
        }

        private void checkBoxUseDefaultCredentials_CheckedChanged(object sender, EventArgs e)
        {
            _dirty = true;
            _useDefaultCredentials = checkBoxUseDefaultCredentials.Checked;
        }

        #endregion

        #region Validate TextBoxes

        private void textBoxProjectServerUrl_Validating(object sender, CancelEventArgs e)
        {
            string errorMsg;
            if (!ValidUrl(textBoxProjectServerUrl.Text, out errorMsg))
            {
                // Cancel the event and select the text to be corrected by the user.
                e.Cancel = true;
                textBoxProjectServerUrl.Select(0, textBoxProjectServerUrl.Text.Length);

                // Set the ErrorProvider error with the text to display. 
                this.errorProvider1.SetError(textBoxProjectServerUrl, errorMsg);
            }
        }

        private void textBoxProjectServerUrl_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(textBoxProjectServerUrl, "");
        }

        public static bool ValidUrl(string url, out string errorMessage)
        {
            // Confirm that the e-mail address string is not empty.
            if (url.Length == 0)
            {
                errorMessage = "Url is required.";
                return false;
            }

            // Confirm that the adress starts with http://
            if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
            {
                errorMessage = "";
                return true;
            }

            errorMessage = "Url must be a valid address format.\n" +
               "For example 'http://www.microsoft.com/project' ";
            return false;
        }


        #endregion

    }
}
